package com.dantfb.services;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hsqldb.lib.Iterator;

import Post.Post;

public class PostsService{
	
	/** The posts. */
	private List<Post> posts;
	
	/** The count post. */
	private int countPost;
	
	public PostsService(List<Post> posts) {
		super();
		this.posts = new LinkedList<Post>();
		this.posts = posts;
		this.countPost = posts.size();
	}

	/**
	 * Gets the count post.
	 *
	 * @return the count post
	 */
	public int getCountPost() {
		return countPost;
	}

	/**
	 * Gets the post.
	 *
	 * @return the post
	 */
	public List<Post> getPost(){
		return this.posts;
	}
	
	/**
	 * Sets the post.
	 *
	 * @param posts the new post
	 */
	public void setPost(List<Post> posts) {
		this.posts = posts;
	}
	
	/**
	 * Creates the post.
	 *
	 * @param message the message
	 */
	public void createPost(String message){
		Iterator itr = (Iterator) posts.iterator();
		while(itr.hasNext()){
			if(itr.next()==null){
				posts.add(new Post(null,null,null,null,null,null,null,null));
			}
		}
	}
	
	/**
	 * Gets the message.
	 *
	 * @param id the id
	 * @return the message
	 */
	public String getPost(int id){
		Iterator itr = (Iterator) posts.iterator();
		String message = null;
		while(itr.hasNext()){
			message = posts.get(id).getMessage();
		}
		Pattern p = Pattern.compile("<div></div>");
		Matcher m = p.matcher(message);
		if(m.find()) return "Message : " + message;
		else return "Contenu vide";
	}
	
	/**
	 * Removes the post.
	 *
	 * @param id the id
	 */
	public void removePost(int id){
		Iterator itr = (Iterator) posts.iterator();
		while(itr.hasNext()){
			posts.remove(id);
		}
	}
	
	//ajouter commentaire ou like
	//rechercher nombre de commentaire
	//reechercher nombre de commentaire
}
